package ar.uba.dc.tl.mylanga.interpreter;

import java.util.ArrayList;
import java.util.List;

public class ExecutionStack {

	private List<Object> stack;

	public ExecutionStack() {
		this.stack = new ArrayList<>();
	}

	public void push(Object object) {
		assert object != null;

		this.stack.add(object);
	}

	public int getSP() {
		return this.stack.size() - 1;
	}

	public Object get(int index) {
		return this.stack.get(index);
	}

	public void set(int index, Object value) {
		this.stack.set(index, value);
	}

	public Object peek() {
		return this.stack.get(this.stack.size() - 1);
	}

	public int peekInteger() {
		return (int)this.peek();
	}

	public double peekDouble() {
		return (double)this.peek();
	}

	public boolean peekBoolean() {
		return (boolean)this.peek();
	}

	public Object pop() {
		return this.stack.remove(this.stack.size() - 1);
	}

	public int popInteger() {
		return (int)this.pop();
	}

	public double popDouble() {
		return (double)this.pop();
	}

	public boolean popBoolean() {
		return (boolean)this.pop();
	}

	@Override
	public String toString() {
		StringBuilder buffer = new StringBuilder();

		buffer.append('[');

		if (this.stack.size() > 10) {
			buffer.append("..., ");
		}

		for (int index = Math.max(0, this.stack.size() - 10); index < this.stack.size(); ++index) {
			buffer.append(this.stack.get(index));
			buffer.append(", ");
		}

		buffer.delete(buffer.length() - 2, buffer.length() - 1);

		buffer.append(']');

		return buffer.toString();
	}
}
